
class VisitorFilters {

	def filters = {
       loginCheck(uri:'/**') {
           after = {
        	  def uas = request.getHeader("User-Agent")   
        	  if(uas == null) uas = "unknown" 
              UserAgent ua = UserAgent.findByName(uas)
              if(ua == null) {
            	  ua = new UserAgent(name : uas)
            	  ua.save();
              }
        	  
        	  def via = request.getHeader("HTTP_VIA")   
        	  def xForwardedFor = request.getHeader("HTTP_X_FORWARDED_FOR")          	  
        	  Visit visit = new Visit(via:via, forwardedFor : xForwardedFor, userAgent : ua)        	  
        	  
        	  def ips = request.remoteAddr
        	  Visitor visitor = Visitor.findByIp(ips)
              if(visitor == null) {
            	  visitor = new Visitor()
            	  visitor.ip = ips 
            	  try {
            		  visitor.hostName = java.net.Inet4Address.getByName(ips).hostName
            	  } catch(e) {}            	  
            	  visitor.save();
              }
        	  visit.visitor = visitor;
        	  

        	  def ref = request.getHeader("Referer")
        	  if(ref) {
	        	  Referer referer = Referer.findByName(ref)
	              if(referer == null) {
	            	  referer = new Referer(name : ref)
	            	  referer.save();
	              }      
	        	  visit.referer = referer;
        	  }
        	  

              visit.save(flush:true);
           }	
       }	
	}
}